{% extends 'base.html' %}

{% load static %}

{% block head_title %}Home{% endblock %}
{% block nosubheader %} {% endblock nosubheader %}
{% block title %}{% endblock title %}

{% block hero_subheader %}
    <div class="jumbotron">
        <div class="container">
            <div class="row">
                <div class="col-sm-12 col-md-8 col-md-offset-2">
                    <img src="{% static "img/codalab-logo-onecolor-reverse.png" %}" alt="CodaLab" class="img-responsive">
                </div>
            </div>
        </div>
    </div>
{% endblock hero_subheader %}

{% block content %}
    <div class="container">
        {% if front_page_message and front_page_message != 'None' %}
            <div class="row justify-content-md-center">
                <div class="col-md-1"></div>
                <div class="col-md-10">
                    <div style="margin-top: 15px;" class="well">
                        {{ front_page_message }}
                    </div>
                </div>
                <div class="col-md-1"></div>
            </div>
        {% endif %}

        <div class="row">
          <div class="col-sm-6 competition-latest-list">
            <a href="/competitions/"><h2 class="homepage_headers">Popular Competitions</h2></a>
            {% if not latest_competitions %}
            <p><em>There are no competitions.</em></p>
            {% else %}
                {% for competition in latest_competitions %}
                    {% include "web/competition_latest.html" with competition=competition %}
                {% endfor %}
            {% endif %}
          </div>
            <div class="col-sm-6 competition-latest-list">
            <a href="/competitions/"><h2 class="homepage_headers">Featured Competitions</h2></a>
            {% if not featured_competitions %}
            <p><em>There are no competitions.</em></p>
            {% else %}
                {% for competition in featured_competitions %}
                    {% include "web/competition_latest.html" with competition=competition %}
                {% endfor %}
            {% endif %}
          </div>
        </div>
    </div>
{% endblock content %}
<script>
{% block js %}
    $(function () {
        $(".top-bar-section ul > li").removeClass("active");
    });

    function populate(frm, data) {
        $.each(data, function(key, value){
            var $ctrl = $('[name='+key+']', frm);
            switch($ctrl.attr("type"))
            {
                case "text":
                case "hidden":
                    $ctrl.val(value);
                    break;
                case "radio":
                case "checkbox":
                    $ctrl.each(function(){
                        if(value=='on') {
                            $(this).attr("checked",value);
                        }
                    });
                    break;
                default:
                    $ctrl.val(value);
            }
        });
    }
    $('.competition-tile').on('click',function(){
        var competitionID = $(this).find('#competitionID').val();
        window.location.href = '/competitions/' + competitionID;
    });

    var params = {}
    location.search.substr(1).split("&").forEach(function(item) {params[item.split("=")[0]] = item.split("=")[1]})

    populate('#search_form', params);
{% endblock js %}
</script>
